package io.github.wslxm.springbootplus2.manage.login.strategy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.wslxm.springbootplus2.core.config.error.ErrorException;
import io.github.wslxm.springbootplus2.core.enums.BaseDic;
import io.github.wslxm.springbootplus2.core.result.ResultType;
import io.github.wslxm.springbootplus2.core.utils.XjBase64Util;
import io.github.wslxm.springbootplus2.core.utils.validated.XjValidUtil;
import io.github.wslxm.springbootplus2.manage.login.model.dto.LoginDTO;
import io.github.wslxm.springbootplus2.manage.login.model.dto.PhonePasswordLoginDTO;
import io.github.wslxm.springbootplus2.manage.login.strategy.service.LoginStrategy;
import io.github.wslxm.springbootplus2.manage.login.util.XjLoginUtil;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysUser;
import io.github.wslxm.springbootplus2.manage.sys.service.SysUserService;
import io.github.wslxm.springbootplus2.utils.XjMd5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 电话+密码 登录
 *
 * @author wangsong
 * @version 1.0.0
 * @mail 1720696548@qq.com
 * @date 2022/10/15 0015 18:18
 */
@Service
public class PhonePasswordLoginStrategy implements LoginStrategy {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private XjLoginUtil xjLoginUtil;

    @Override
    public SysUser getSysUser(LoginDTO dto) {
        PhonePasswordLoginDTO loginDTO = JSON.parseObject(JSON.toJSONString(dto.getData()), PhonePasswordLoginDTO.class);
        XjValidUtil.isNull(loginDTO, ResultType.BASE_NO_REQ_PARAM.getMsg());
        String phone = loginDTO.getPhone();
        String password = XjBase64Util.decrypt(loginDTO.getPassword());
        XjValidUtil.isBlank(phone, "login.phone.NotBlank");
        XjValidUtil.isStrLen(phone, 11, 11, "login.phone.Length");
        XjValidUtil.isBlank(password, "login.password.NotBlank");
        XjValidUtil.isStrLen(password, 1, 20, "login.password.Length");
        List<SysUser> users = sysUserService.list(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getPhone, phone)
        );
        // 判断账号
        XjValidUtil.isTrue(users.isEmpty(),"login.username.not");
        return users.get(0);
    }

    @Override
    public void login(LoginDTO dto,SysUser user) {
        // 检查登录环境
        XjValidUtil.isTrue(!xjLoginUtil.isCaptchaVerify(user, dto.getCaptchaId()), "login.captchaId.verify");
        // 开始登录
        PhonePasswordLoginDTO loginDTO = JSON.parseObject(JSON.toJSONString(dto.getData()), PhonePasswordLoginDTO.class);
        String password = XjBase64Util.decrypt(loginDTO.getPassword());
        // 1、判断密码以及错误次数
        long inPwaCount = xjLoginUtil.getPwaInCount(user, null);
        XjValidUtil.isTrue(inPwaCount <= 0, "login.password.failCount");
        if (!user.getPassword().equals(XjMd5Util.encode(password, user.getId()))) {
            xjLoginUtil.pwdFailRecord(user);
            throw new ErrorException("login.password.error");
        }
        xjLoginUtil.cleanPwdFailRecord(user);
        // 判断禁用
        XjValidUtil.isTrue(user.getDisable().equals(BaseDic.Whether.V1.getValue()),"login.username.disable");
        // 设置登录环境
        xjLoginUtil.setLoginEnvironment(user, null);
    }
}
